﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using sinetd;
using sinetd.Protocols;

namespace ChatGUI
{
    public partial class LoginForm : Form
    {
        private Chat chat;

        public LoginForm(Chat chat, string cfgFileName)
        {
            this.chat = chat;

            InitializeComponent();
            this.Icon = sinetd.Utilities.Assembly.GetIconLarge(sinetd.Utilities.Process.GetExeName(), 0);
            this.pictureBox1.Image = this.Icon.ToBitmap();

            Configuration cfg = new Configuration();
            cfg.Load(cfgFileName);

            cb_conn.DisplayMember = "Item1";
            cb_conn.DropDownStyle = ComboBoxStyle.DropDownList;
            foreach (KeyValuePair<string, IManagerElement> kvp in cfg.Protocols)
            {
                cb_conn.Items.Add(new Tuple<string, IProtocol>(kvp.Value.Name + " [" + kvp.Value.Config + "]", (IProtocol)kvp.Value));
            }
            if (cb_conn.Items.Count > 0) cb_conn.SelectedIndex = 0;
        }

        private void button_login_Click(object sender, EventArgs e)
        {
            if (cb_conn.Items.Count > 0)
            {
                this.chat.Protocol = (IProtocol)(cb_conn.SelectedItem as Tuple<string, IProtocol>).Item2;

                if (chat.Protocol.Connect())
                {
                    if (chat.Login(textBox1.Text))
                    {
                        Close();
                    }
                }
            }
        }
    }
}
